package com.evil.common.core.entity;

import com.alibaba.fastjson.JSON;
import com.evil.common.core.constant.AppConstant;
import jakarta.validation.Valid;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Optional;

/**
 * 分页查询-有序ID查询
 *
 * @author liyang
 * @date 2025-01-08 18:00
 */
@Data
@NoArgsConstructor
public class PageParam<ID extends Number, T> {

    /**
     * 页尺
     */
    private int pageSize = AppConstant.DEFAULT_PAGE_SIZE;
    /**
     * 上一个ID
     */
    private ID lastId;
    /**
     * 正序
     */
    private boolean asc = true;
    /**
     * 查询条件
     */
    @Valid
    private T param;

    public PageParam(T param) {
        this.param = param;
    }

    public PageParam(ID lastId, T param) {
        this.lastId = lastId;
        this.param = param;
    }

    public PageParam(int pageSize, T param) {
        this.pageSize = pageSize;
        this.param = param;
    }

    public PageParam(int pageSize, ID lastId, T param) {
        this.pageSize = pageSize;
        this.lastId = lastId;
        this.param = param;
    }

    public Optional<T> getCheckParam() {
        return Optional.ofNullable(param);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
